//
//  EnvironmentObserverViewController.m
//  Rail
//
//  Created by hychen on 2018/3/6.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "EnvironmentObserverViewController.h"
#import "EnvironmentHeaderView.h"
#import "EnvironmentChartTableViewCell.h"
@interface EnvironmentObserverViewController ()
@property (nonatomic, strong) NSMutableArray *chartArray;
@end

@implementation EnvironmentObserverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:[NSString stringWithFormat:@"%@环境监测",self.detailModel.area_name?:@""]];
    [self.tableView.mj_header beginRefreshing];
}

- (void)setTableHeaderView:(EnvironmentHeaderModel *)model {
    EnvironmentHeaderView *headerView = [[EnvironmentHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 200)];
    headerView.model = model;
    self.tableView.tableHeaderView = headerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    self.currentPage = 1;
    BMWeakSelf
    [HttpRequest loadEnvironmentRecordListWithAreaId:self.detailModel.areaid pageNo:[@(self.currentPage) stringValue] success:^(id object) {
        if ([object isKindOfClass:[NSArray class]]) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
        }
        [weakSelf setupTableViewData];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_footer resetNoMoreData];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)setupTableViewData {
//    if (self.dataArray.count < 1) {
//        return;
//    }
    
    EnvironmentChartModel *temperatureChart = [EnvironmentChartModel new];
    temperatureChart.title = @"温度";
    EnvironmentChartModel *wetnessChart = [EnvironmentChartModel new];
    wetnessChart.title = @"湿度";
    EnvironmentChartModel *noiseChart = [EnvironmentChartModel new];
    noiseChart.title = @"噪音";
    EnvironmentChartModel *pmChart = [EnvironmentChartModel new];
    pmChart.title = @"PM2.5";
    EnvironmentChartModel *pm10Chart = [EnvironmentChartModel new];
    pm10Chart.title = @"PM10";
    EnvironmentChartModel *wsChart = [EnvironmentChartModel new];
    wsChart.title = @"风力";
    NSMutableArray *chartArray = [NSMutableArray arrayWithObjects:temperatureChart,wetnessChart,noiseChart,pmChart,pm10Chart,wsChart, nil];
    for (int i = 0; i < self.dataArray.count; ++i) {
        EnvironmentObserverModel *model = [self.dataArray objectAtIndex:i];
        //添加时间
        for (EnvironmentChartModel *chart in chartArray) {
            if (model.record_time.length>=19) {
                [chart.timeArray addObject:[model.record_time substringWithRange:NSMakeRange(11, 5)]];
            }
            else {
                [chart.timeArray addObject:model.record_time];
            }
            
        }
        [temperatureChart.dataArray addObject:model.temperature?:@""];
        [wetnessChart.dataArray addObject:model.wetness?:@""];
        [noiseChart.dataArray addObject:model.noise?:@""];
        [pmChart.dataArray addObject:model.pm?:@""];
        [pm10Chart.dataArray addObject:model.pm10?:@""];
        [wsChart.dataArray addObject:model.ws?:@""];
    }
    self.chartArray = chartArray;
    
    NSString *title = self.detailModel.area_name;
    EnvironmentObserverModel *model = self.dataArray.firstObject;
    EnvironmentHeaderModel *headerModel = [EnvironmentHeaderModel new];
    headerModel.title = title;
    headerModel.dataModel = model;
    [self setTableHeaderView:headerModel];
    [self.tableView reloadData];
}

//- (void)loadMoreData {
//    self.currentPage++;
//    BMWeakSelf
//    [HttpRequest loadEnvironmentRecordListWithAreaId:self.detailModel.areaid pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
//        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
//            [weakSelf.dataArray addObjectsFromArray:object];
//            [weakSelf.tableView.mj_footer endRefreshing];
//            [weakSelf.tableView reloadData];
//        }
//        else {
//            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
//    } failure:^(NSString *code, NSString *msg) {
//        Show_Msg_Error(msg)
//        [weakSelf.tableView.mj_footer endRefreshing];
//    }];
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.chartArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EnvironmentChartModel *model = [self.chartArray objectAtIndex:indexPath.row];
    EnvironmentChartTableViewCell *cell = [EnvironmentChartTableViewCell cellWithInitStyleTableView:tableView class:[EnvironmentChartTableViewCell class]];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}

@end
